import Path = require('path');
import less = require('less');
import CssLoader from './CssLoader';
import File from '../file/File';

export default class LessLoader extends CssLoader {
    async load(file: File) {
        let options: Less.Options = {
            paths: [Path.dirname(file.src || '')],
            filename: Path.basename(file.src || '')
        };
        if (file.cx.options.less) {
            Object.assign(options, file.cx.options.less);
        }
        let code = await less.render(file.contents, options);
        if (code.css) {
            file.contents = code.css;
        }
        await super.load(file);
    }
}